package com.lmy.androidcustomview.view

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
import com.lmy.androidcustomview.R
import com.lmy.androidcustomview.dp

/**
 * @author
 * @功能:
 * @Creat 2/11/21 12:18 PM
 * @Compony 465008238@qq.com
 */
class MaterialEditText(context: Context, attributeSet: AttributeSet) :
    AppCompatEditText(context, attributeSet) {
    val TEXT_SIZE = 12.dp//Label文字大小
    val TEXT_MARGIN = 8.dp//文字的Margin
    val HORIZONTAL_OFFSET = 5.dp//Label距离左边的偏移量
    val VERTICAL_OFFSET = 23.dp//Label距离顶部的偏移量
    val EXTRA_VERTICLA_OFFSET = 10 //Label的可移动距离
    var isFloatingLabel = false
        set(value) {
            if (field !== value) {
                field = value
                if (field) {
                    //设置顶部距离 留出文字空隙
                    setPadding(
                        paddingLeft,
                        (paddingTop + TEXT_SIZE + TEXT_MARGIN).toInt(),
                        paddingRight,
                        paddingBottom
                    )
                } else {
                    //设置顶部距离 留出文字空隙
                    setPadding(
                        paddingLeft,
                        (paddingTop - TEXT_SIZE - TEXT_MARGIN).toInt(),
                        paddingRight,
                        paddingBottom
                    )
                }
            }
        }
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var floatingLabelShow = false//判断输入之前的文字是否为空
    private var floatingLabelFraction = 0f
        //动画完成的进度
        set(value) {
            field = value
            invalidate()
        }

    /**
     *Label显示动画
     */
    private val animate by lazy {
        ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f)
    }

    init {
        paint.textSize = TEXT_SIZE
        val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.MaterialEditText)
        isFloatingLabel = typedArray.getBoolean(R.styleable.MaterialEditText_isFloatingLabel, true)
    }

    override fun onTextChanged(
        text: CharSequence,
        start: Int,
        lengthBefore: Int,
        lengthAfter: Int
    ) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter)
        if (isFloatingLabel){
            if (floatingLabelShow && text.isNullOrEmpty()) {
                //之前为空和输入为空则执行消失动画
                floatingLabelShow = false
                animate.reverse()
            } else if (!floatingLabelShow && !text.isNullOrEmpty()) {
                floatingLabelShow = true
                animate.start()
            }
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //根据动画完成度来计算透明度
        paint.alpha = (floatingLabelFraction * 0xff).toInt()
        //根据动画完成度计算Label的顶部偏移值 因为这里是越往上越小所以需要*1 - 完成进度 否则就是从上往下做动画了
        val current = VERTICAL_OFFSET + EXTRA_VERTICLA_OFFSET * (1 - floatingLabelFraction)
        canvas.drawText(hint.toString(), HORIZONTAL_OFFSET, current, paint)
    }
}